/*
 * uhfRadio.cpp
 *
 *  Created on: 29.09.2012
 *      Author: Wolfgang Engelhard
 *
 * Copyright: (c) 2011 by Wolfgang Engelhard
 * License: GNU GPL v2 (see License.txt)
 */
#include <inttypes.h>
#include <cstdio>
#include "uhfRadio.h"

/**
 * converts frequency string and channel string to UHF-Radio data format
 */
int convert(uint16_t pBuffer[2], char *pFreq, char *pChan) {

	if (strlen(pFreq) != 6) {
		return -1;
	}
	if (strlen(pChan) != 2) {
		return -1;
	}
	pBuffer[0] = (pFreq[0] << 4) & 0x0030;
	pBuffer[0] |= pFreq[1] & 0x0F;
	pBuffer[0] |= (pFreq[2] << 11) & 0x7800;
	pBuffer[0] |= (pFreq[3] << 7) & 0x0700;
	pBuffer[0] |= (pFreq[3] << 6) & 0x0040;
	pBuffer[1] = 0;
	if (pFreq[4] > '2') {
		pBuffer[1] = 0x0040;
	}
	if (pFreq[5] == '5') {
		pBuffer[1] |= 0x0020;
	}
	pBuffer[1] |= atoi(pChan) & 0x001F;

	return 0;
}
